package cqut.wys.web;

import com.github.pagehelper.PageHelper;
import cqut.wys.dao.entity.WfUser;
import cqut.wys.dao.entity.WfUserExample;
import cqut.wys.dao.mapper.WfUserMapper;
import cqut.wys.dto.Dto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * --------神兽出没--------/
 * ┌─┐     ┌─┐
 * ┌─┘─┴─────┘─┴─┐
 * │      ─      │
 * │  ┬─┘   └─┬  │
 * │             │
 * │      ┴      │  Code is far away from bug with the animal protecting
 * └───┐      ┌──┘  神兽保佑,代码无bug
 *     │      │
 *     │      └──────┐
 *     │             ├┐
 *     │             ┌┘
 *     └┐ ┐ ┌───┬─┐ ┌┘
 *      │ ┤ ┤   │ ┤ ┤
 *      └─┴─┘   └─┴─┘
 * --------感觉萌萌哒--------/
 *
 * @author wuyoushan
 * @date 2018/4/6
 */
@Controller
@RequestMapping("/wf")
public class WfUserController {

    @Autowired
    private WfUserMapper wfUserMapper;

    /**
     * 用户列表
     */
    @GetMapping("/user/list")
    public String listWfUser(ModelMap map) {
        List<WfUser> list = getListWfUser();
        map.addAttribute("list",list);
        return "wfUser/user-list";
    }

    /**
     * 添加用户
     */
    @GetMapping("/user/save")
    public String saveWfUser(){
        return "wfUser/save";
    }

    /**
     * 保存用户
     */
    @PostMapping("/user/save")
    public int saveWfUser(WfUser wfUser){
        return wfUserMapper.insertSelective(wfUser);
    }


    /**
     * 列表树
     */
    @GetMapping("/tree/list")
    public String listTreeWfUser() {
        return "wfUser/tree-list";
    }

    @GetMapping("/user/update/{id}")
    public String updateWfUser(@RequestParam("id") int id, ModelMap map){
        WfUser wfUser = wfUserMapper.selectByPrimaryKey(id);
        map.put("user",wfUser);
        return "wfUser/update";
    }

    /**
     * 获取族谱树数据
     */
    @ResponseBody
    @GetMapping("/getDto")
    public Dto getDto(){
        Map<Integer, List<WfUser>> map = parpereMap(getListWfUser());
        Dto dto = new Dto();
        dto.setParentId(0);
        dto.setName(map.get(0).get(0).getUserName());
        dto.setChildren(prepareDtoList(map,map.get(1)));
        return dto;
    }

    /**
     * 查询用户列表
     */
    private List<WfUser> getListWfUser(){
        PageHelper.startPage(1,20);
        WfUserExample wfUserExample = new WfUserExample();
        wfUserExample.createCriteria().andIsDeletedEqualTo((byte) 0);
        List<WfUser> list = wfUserMapper.selectByExample(wfUserExample);
        return list;
    }

    /**
     * 组装DtoList（递归实现）
     *
     * @param map map
     * @param list list
     */
    private List<Dto> prepareDtoList (Map<Integer, List<WfUser>> map,List<WfUser> list){
        List<Dto> dtoList = new ArrayList<>();
        for (WfUser wfUser : list) {
            int userId = wfUser.getUserId();
            Dto dto = new Dto();
            dto.setParentId(wfUser.getUserParentId());
            dto.setName(wfUser.getUserName());
            if (map.containsKey(userId)){
                dto.setChildren(prepareDtoList(map,map.get(userId)));
            }
            dtoList.add(dto);
        }
        return dtoList;
    }

    /**
     * 根据父节点组装
     *
     * @param list List<WfUser>对象
     */
    private Map<Integer, List<WfUser>> parpereMap(List<WfUser> list) {
        Map<Integer, List<WfUser>> map = new HashMap<>();
        for (WfUser wfUser : list) {
            int userParentId = wfUser.getUserParentId();
            if (map.containsKey(userParentId)) {
                List<WfUser> oldWfUserList = map.get(userParentId);
                oldWfUserList.add(wfUser);
                map.put(userParentId, oldWfUserList);
            } else {
                List<WfUser> newWfUserList = new ArrayList<>();
                newWfUserList.add(wfUser);
                map.put(userParentId, newWfUserList);
            }
        }
        return map;
    }
}
